08 / 14

What is the purpose of the `Array.isArray()` method?

The Array.isArray() method is used to determine whether a given value is an array. It returns true if the value is an array, and false otherwise.

Example code:
Difficulty: 3/10
Topics: type checking, runtime validation, array detection

Scenario Questions

0-2 years experience
  1. 1

    You receive a JSON payload where a field might be a single object or an array of objects. How would you use Array.isArray to handle both cases safely?

  2. 2

    What does Array.isArray([]) return and why?

  3. 3

    If you call Array.isArray on a string value, what result do you get?

2-5 years experience
  1. 1

    Your team replaced a custom check (typeof value === 'object') with Array.isArray and a bug disappeared. Explain why the original check was insufficient.

  2. 2

    During debugging you see a NodeList being passed to a function that expects an array, causing .map to fail. How would you detect and convert it safely?

  3. 3

    When merging API responses you sometimes get undefined instead of an array. Show how you’d guard the concatenation using Array.isArray.

5-8 years experience
  1. 1

    In a high‑throughput data pipeline you need to verify that incoming fields are arrays. Discuss the performance impact of using Array.isArray versus other checks at scale and any optimization you’d consider.

  2. 2

    You’re designing a shared library function that accepts either a single item or an array of items. How would you structure its API and internal validation with Array.isArray to keep it robust across services?

  3. 3

    Your code runs in both Node and browser bundles. Explain how reliance on Array.isArray affects bundle size and tree‑shaking, and whether any alternatives are viable.

8+ years experience
  1. 1

    Your organization is migrating legacy code that uses custom isArray helpers to a new TypeScript codebase. Outline a migration plan that replaces those helpers with Array.isArray, covering backward compatibility, testing, and cross‑team coordination.

  2. 2

    You need to enforce a contract across multiple microservices that certain payload fields must be arrays. How would you standardize validation using Array.isArray while keeping services loosely coupled?

  3. 3

    When building a schema‑validation framework that runs in Node, Deno, and browsers, discuss how you’d abstract array detection and why you’d still choose Array.isArray as the canonical check.

Follow-up Questions

  • What edge cases would you test for when using Array.isArray?
  • How does Array.isArray behave with values from different iframes or windows?
  • If you needed to support older browsers without Array.isArray, what fallback would you use?